﻿
namespace Indeco.G24.Windows.Forms
{
	/// <summary>
	/// abstract NguoiDungDiaBan typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class NguoiDungDiaBanDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<NguoiDungDiaBanDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.NguoiDungDiaBan _currentNguoiDungDiaBan = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxNguoiDungDiaBanDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxNguoiDungDiaBanErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxNguoiDungDiaBanBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the IDDiaBan property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDDiaBanDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDNguoiDung property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDNguoiDungDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.NguoiDung> _IDNguoiDungList;
		
		/// <summary> 
		/// The list of selectable NguoiDung
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.NguoiDung> IDNguoiDungList
		{
			get {return this._IDNguoiDungList;}
			set 
			{
				this._IDNguoiDungList = value;
				this.uxIDNguoiDungDataGridViewColumn.DataSource = null;
				this.uxIDNguoiDungDataGridViewColumn.DataSource = this._IDNguoiDungList;
			}
		}
		
		private bool _allowNewItemInIDNguoiDungList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of NguoiDung
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDNguoiDungList
		{
			get { return _allowNewItemInIDNguoiDungList;}
			set
			{
				this._allowNewItemInIDNguoiDungList = value;
				this.uxIDNguoiDungDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.DiaBan> _IDDiaBanList;
		
		/// <summary> 
		/// The list of selectable DiaBan
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.DiaBan> IDDiaBanList
		{
			get {return this._IDDiaBanList;}
			set 
			{
				this._IDDiaBanList = value;
				this.uxIDDiaBanDataGridViewColumn.DataSource = null;
				this.uxIDDiaBanDataGridViewColumn.DataSource = this._IDDiaBanList;
			}
		}
		
		private bool _allowNewItemInIDDiaBanList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of DiaBan
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDDiaBanList
		{
			get { return _allowNewItemInIDDiaBanList;}
			set
			{
				this._allowNewItemInIDDiaBanList = value;
				this.uxIDDiaBanDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.NguoiDungDiaBan> _NguoiDungDiaBanList;
				
		/// <summary> 
		/// The list of NguoiDungDiaBan to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.NguoiDungDiaBan> NguoiDungDiaBanList
		{
			get {return this._NguoiDungDiaBanList;}
			set
			{
				this._NguoiDungDiaBanList = value;
				this.uxNguoiDungDiaBanBindingSource.DataSource = null;
				this.uxNguoiDungDiaBanBindingSource.DataSource = value;
				this.uxNguoiDungDiaBanDataGridView.DataSource = null;
				this.uxNguoiDungDiaBanDataGridView.DataSource = this.uxNguoiDungDiaBanBindingSource;				
				//this.uxNguoiDungDiaBanBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxNguoiDungDiaBanBindingSource_ListChanged);
				this.uxNguoiDungDiaBanBindingSource.CurrentItemChanged += new System.EventHandler(OnNguoiDungDiaBanBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnNguoiDungDiaBanBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentNguoiDungDiaBan = uxNguoiDungDiaBanBindingSource.Current as Entities.NguoiDungDiaBan;
			
			if (_currentNguoiDungDiaBan != null)
			{
				_currentNguoiDungDiaBan.Validate();
			}
			//_NguoiDungDiaBan.Validate();
			OnCurrentEntityChanged();
		}

		//void uxNguoiDungDiaBanBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.NguoiDungDiaBan"/> instance.
		/// </summary>
		public Entities.NguoiDungDiaBan SelectedNguoiDungDiaBan
		{
			get {return this._currentNguoiDungDiaBan;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxNguoiDungDiaBanDataGridView.VirtualMode;}
			set
			{
				this.uxNguoiDungDiaBanDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxNguoiDungDiaBanDataGridView.AllowUserToAddRows;}
			set {this.uxNguoiDungDiaBanDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxNguoiDungDiaBanDataGridView.AllowUserToDeleteRows;}
			set {this.uxNguoiDungDiaBanDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxNguoiDungDiaBanDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxNguoiDungDiaBanDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="NguoiDungDiaBanDataGridViewBase"/> class.
		/// </summary>
		public NguoiDungDiaBanDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxNguoiDungDiaBanDataGridView = new System.Windows.Forms.DataGridView();
			this.uxNguoiDungDiaBanBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxNguoiDungDiaBanErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDiaBanDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDNguoiDungDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			//this.uxIDNguoiDungBindingSource = new NguoiDungBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNguoiDungBindingSource)).BeginInit();
			//this.uxIDDiaBanBindingSource = new DiaBanBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDDiaBanBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungDiaBanDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungDiaBanBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungDiaBanErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxNguoiDungDiaBanErrorProvider
			// 
			this.uxNguoiDungDiaBanErrorProvider.ContainerControl = this;
			this.uxNguoiDungDiaBanErrorProvider.DataSource = this.uxNguoiDungDiaBanBindingSource;						
			// 
			// uxNguoiDungDiaBanDataGridView
			// 
			this.uxNguoiDungDiaBanDataGridView.AutoGenerateColumns = false;
			this.uxNguoiDungDiaBanDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxNguoiDungDiaBanDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDiaBanDataGridViewColumn,
		this.uxIDNguoiDungDataGridViewColumn			});
			this.uxNguoiDungDiaBanDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxNguoiDungDiaBanDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxNguoiDungDiaBanDataGridView.Name = "uxNguoiDungDiaBanDataGridView";
			this.uxNguoiDungDiaBanDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxNguoiDungDiaBanDataGridView.TabIndex = 0;	
			this.uxNguoiDungDiaBanDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxNguoiDungDiaBanDataGridView.EnableHeadersVisualStyles = false;
			this.uxNguoiDungDiaBanDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnNguoiDungDiaBanDataGridViewDataError);
			this.uxNguoiDungDiaBanDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNguoiDungDiaBanDataGridViewCellValueNeeded);
			this.uxNguoiDungDiaBanDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNguoiDungDiaBanDataGridViewCellValuePushed);
			
			//
			// uxIDDiaBanDataGridViewColumn
			//
			this.uxIDDiaBanDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDiaBanDataGridViewColumn.DataPropertyName = "IDDiaBan";
			this.uxIDDiaBanDataGridViewColumn.HeaderText = "IDDiaBan";
			this.uxIDDiaBanDataGridViewColumn.Name = "uxIDDiaBanDataGridViewColumn";
			this.uxIDDiaBanDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDiaBanDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDiaBanDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDNguoiDungDataGridViewColumn
			//
			this.uxIDNguoiDungDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDNguoiDungDataGridViewColumn.DataPropertyName = "IDNguoiDung";
			this.uxIDNguoiDungDataGridViewColumn.HeaderText = "IDNguoiDung";
			this.uxIDNguoiDungDataGridViewColumn.Name = "uxIDNguoiDungDataGridViewColumn";
			this.uxIDNguoiDungDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDNguoiDungDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDNguoiDungDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDNguoiDungDataGridViewColumn
			//				
			this.uxIDNguoiDungDataGridViewColumn.DisplayMember = "TenSuDung";	
			this.uxIDNguoiDungDataGridViewColumn.ValueMember = "IDNguoiDung";	
			this.uxIDNguoiDungDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDNguoiDungDataGridViewColumn.DataSource = uxIDNguoiDungBindingSource;				
				
			//
			// uxIDDiaBanDataGridViewColumn
			//				
			this.uxIDDiaBanDataGridViewColumn.DisplayMember = "IDDiaBanCha";	
			this.uxIDDiaBanDataGridViewColumn.ValueMember = "IDDiaBan";	
			this.uxIDDiaBanDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDDiaBanDataGridViewColumn.DataSource = uxIDDiaBanBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxNguoiDungDiaBanDataGridView);
			this.Name = "NguoiDungDiaBanDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDNguoiDungBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDDiaBanBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungDiaBanErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungDiaBanDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNguoiDungDiaBanBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				NguoiDungDiaBanDataGridViewEventArgs args = new NguoiDungDiaBanDataGridViewEventArgs();
				args.NguoiDungDiaBan = _currentNguoiDungDiaBan;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class NguoiDungDiaBanDataGridViewEventArgs : System.EventArgs
		{
			private Entities.NguoiDungDiaBan	_NguoiDungDiaBan;
	
			/// <summary>
			/// the  Entities.NguoiDungDiaBan instance.
			/// </summary>
			public Entities.NguoiDungDiaBan NguoiDungDiaBan
			{
				get { return _NguoiDungDiaBan; }
				set { _NguoiDungDiaBan = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxNguoiDungDiaBanDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungDiaBanDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxNguoiDungDiaBanDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungDiaBanDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNguoiDungDiaBanDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDiaBanDataGridViewColumn":
						e.Value = NguoiDungDiaBanList[e.RowIndex].IDDiaBan;
						break;
					case "uxIDNguoiDungDataGridViewColumn":
						e.Value = NguoiDungDiaBanList[e.RowIndex].IDNguoiDung;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxNguoiDungDiaBanDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNguoiDungDiaBanDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNguoiDungDiaBanDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDiaBanDataGridViewColumn":
						NguoiDungDiaBanList[e.RowIndex].IDDiaBan = (System.Guid)e.Value;
						break;
					case "uxIDNguoiDungDataGridViewColumn":
						NguoiDungDiaBanList[e.RowIndex].IDNguoiDung = (System.Guid)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
